package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Order;
import com.mall.db.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/raeo")
public class RAEOrderController extends BaseController<Order> {

    @Resource(name = "orderService")
    private OrderService os;

    @RequestMapping("/yes")
    public Map<String,Object> yes(Order order){
        Map<String,Object> map=new HashMap<>();
        if("退货".equals(order.getState())){
            boolean b= os.removeById(order.getId());
            map.put("is",b);
            map.put("msg","成功退货");
        }else if("换货".equals(order.getState())){
            order.setState("待发货");
            order.setOldCommodity(order.getNewCommodity());
            order.setNewCommodity("");
            os.updateById(order);
            map.put("is",true);
            map.put("msg","成功换货");
        }

        return map;
    }


    @RequestMapping("/no")
    public Map<String,Object> no(Order order){
        Map<String,Object> map=new HashMap<>();
        if("退货".equals(order.getState())){
            order.setState("待收货");
            os.updateById(order);
            map.put("is",true);
            map.put("msg","不同意退货");
        }else if("换货".equals(order.getState())){
            order.setState("待收货");
            os.updateById(order);
            map.put("is",true);
            map.put("msg","不同意换货");
        }

        return map;
    }
    @Override
    public Map<String, Object> find(Order order) {
        PageHelper.startPage(order.getPage(),order.getLimit());
         QueryWrapper qw=new QueryWrapper();
         qw.in("state","退货","换货");
         List<Order> list=os.list(qw);
        PageInfo<Order> pageInfo=new PageInfo<>(list,order.getLimit());
        return this.findMap(pageInfo.getList(),pageInfo.getTotal()) ;
    }

}
